package com.tarena.elts.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.tarena.elts.entity.User;

public class MenuFrame extends JFrame{
	private JLabel infoLabel;
	private ClientContext clientContext;
	public void setClientContext(ClientContext clientContext) {
		this.clientContext = clientContext;
	}
	public MenuFrame(){
		this.init();
	}
	private void init(){
		this.setSize(600,400);
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				clientContext.exit(MenuFrame.this);
			}
		});
		this.setLocationRelativeTo(null);
		this.add(createContentPanel());
	}
	private JPanel createContentPanel(){
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(BorderLayout.NORTH,createTopPanel());
		panel.add(BorderLayout.CENTER,creatCenterPanel());
		panel.add(BorderLayout.SOUTH,createEndPanel());
		return panel;
	}
	private JPanel createEndPanel() {
		// TODO Auto-generated method stub
		JPanel panel=new JPanel(new BorderLayout());
		JLabel label = new JLabel("All me",JLabel.RIGHT);
		panel.add(BorderLayout.SOUTH,label);
		return panel;
	}
	private JPanel creatCenterPanel() {
		// TODO Auto-generated method stub
		JPanel panel=new JPanel(new FlowLayout());
		JButton start = createImgIcon("exam.png","开始");
		JButton result = createImgIcon("result.png","考试成绩");
		JButton message = createImgIcon("message.png","考试规则");
		JButton exit = createImgIcon("exit.png","离开");
		exit.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				clientContext.exit(MenuFrame.this);
			}
		});
		start.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				clientContext.start();
			}
		});
		result.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				clientContext.result();
			}
		});
		panel.add(start);
		panel.add(result);
		panel.add(message);
		panel.add(exit);
		getRootPane().setDefaultButton(start);

		return panel;
	}
	private JPanel createTopPanel(){
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(BorderLayout.NORTH,createTitlePanel());
		panel.add(BorderLayout.SOUTH,createTipPanel());
		return panel;
	}
	private JPanel createTitlePanel() {
		// TODO Auto-generated method stub
		JPanel panel=new JPanel(new BorderLayout());
		JLabel label = createImgIcon("title.png");
		panel.add(BorderLayout.NORTH,label);
		return panel;
	}
	private JPanel createTipPanel(){
		JPanel panel = new JPanel(new BorderLayout());
//		JLabel label = new JLabel("welcome everybody",JLabel.CENTER);
//		//必加
//		this.infoLabel=label;
		infoLabel = new JLabel("welcome everybody",JLabel.CENTER);
//		panel.add(BorderLayout.SOUTH,label);
		panel.add(BorderLayout.SOUTH,infoLabel);
		return panel;
	}
	private JButton createImgIcon(String img,String txt){
		ImageIcon ico = new ImageIcon(this.getClass().getResource(img));
		JButton button = new JButton(txt,ico);
		button.setVerticalTextPosition(JButton.BOTTOM);
		button.setHorizontalTextPosition(JButton.CENTER);
		return button;
	}
	private JLabel createImgIcon(String img){
		ImageIcon ico = new ImageIcon(this.getClass().getResource(img));
		JLabel label = new JLabel(ico);
		return label;
	}
	/**
	 * 更新用户信息
	 * @param user
	 */
	public void updateView(User user){
		String str = "welcome " + user.getName()+" test";
		infoLabel.setText(str);
	}
}
